<template>
  <div class="edu-page-warp">
    <ConditionCard body-style="padding: 24px 24px 6px;" @search="search" @reset="reset">
      <el-form :model="condition" label-width="100px" ref="conditionForm">
        <el-row :gutter="24">
          <el-col :span="8">
            <el-form-item :label="$t('auditBatch.compeleteGraduationYear') + ':'" prop="graduationYear">
              <el-date-picker
                v-model="condition.graduationYear"
                value-format="yyyy"
                align="right"
                type="year"
                @change="selectChange"
                :placeholder="$t('common.pla.pleaseSelect')"
                clearable
                style="width: 100%" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item :label="$t('auditBatch.auditBatch') + ':'" prop="auditBatch">
              <el-select v-model="condition.auditBatch" style="width: 100%" filterable clearable>
                <el-option v-for="item in options.auditBatch" :key="item.id" :label="item.label" :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </ConditionCard>
    <el-card shadow="never" body-style="padding: 24px;">
      <div class="clearfix" style="margin-bottom:16px">
        <div class="edu-page-title graduationReview-title float-left">{{$t("graduationRuth.completeAduitBatchList")}}</div>
        <div class="float-right">
          <el-button type="primary" @click="operateBatch('open')">{{$t("graduationRuth.batchOpen")}}</el-button>
          <el-button @click="operateBatch('close')">{{$t("graduationRuth.batchClose")}}</el-button>
        </div>
      </div>
      <edu-table :data="tableData" :order="false" :selection="false" @selection-change="checkBoxChange" :pageNum="PaginationParam.pageNum"
        :pageSize="PaginationParam.pageSize" :total="PaginationParam.total" @page-change="pageChange" :isRowClickChangeRowSelection="false">
        <el-table-column type="selection" width="30" :selectable="selectable"/>
        <el-table-column :label="$t('message.order')" tooltip-effect :index="indexMethod" type="index" width="55"
          align="center">
        </el-table-column>
        <edu-table-column :label="$t('auditBatch.compeleteGraduationYear')" width="80" align="center" prop="grade">
        </edu-table-column>
        <edu-table-column :label="$t('graduationRuth.facultyName')" min-width="150" align="center" prop="collegeI18n">
        </edu-table-column>
        <edu-table-column :label="$t('auditBatch.auditBatch')" width="80" align="center" prop="reviewBatchI18n">
        </edu-table-column>
        <edu-table-column :label="$t('graduationRuth.auditTimeArrage')" align="center" min-width="300">
          <template slot-scope="scope">
            <div>{{scope.row.collegeReviewStartDate}}-{{scope.row.collegeReviewEndDate}}</div>
          </template>
        </edu-table-column>
        <edu-table-column :label="$t('graduationRuth.auditAuthStatus')" align="center" min-width="120">
          <template slot-scope="scope">
            <div v-if="auditAuthStatusMap[scope.row.authorityStatus]">
              <el-tag size="mini" :type="auditAuthStatusMap[scope.row.authorityStatus].type">
                {{auditAuthStatusMap[scope.row.authorityStatus].label}}</el-tag>
            </div>
          </template>
        </edu-table-column>
        <!-- <edu-table-column :label="$t('graduationRuth.batchStatus')" align="center" width="80">
          <template slot-scope="scope">
            <div v-if="batchStatusMap[scope.row.batchStatus]">
              <el-tag size="mini" :type="batchStatusMap[scope.row.batchStatus].type">
                {{batchStatusMap[scope.row.batchStatus].label}}</el-tag>
            </div>
          </template>
        </edu-table-column>
        <edu-table-column :label="$t('graduationRuth.batchAutherNumber')" width="100" align="center"
          prop="approver">
        </edu-table-column>
        <edu-table-column :label="$t('graduationRuth.laseBatchTime')" align="center" min-width="150">
          <template slot-scope="scope">
            <div>{{scope.row.endReviewDate}}</div>
          </template>
        </edu-table-column> -->
        <edu-table-column :label="$t('common.text.operation')" align="center" min-width="100">
          <template slot-scope="scope">
            <div v-if="scope.row.authorityStatus=='1'">
              <el-button type="text" size="mini" @click.stop="singelOperate(scope.row)" :disabled="scope.row.isOperateBtnDisabled">{{$t('label.close')}}</el-button>
            </div>
            <div v-else>
              <el-button type="text" size="mini" @click.stop="singelOperate(scope.row, true)" :disabled="scope.row.isOperateBtnDisabled">{{$t('label.open')}}</el-button>
            </div>
          </template>
        </edu-table-column>
      </edu-table>
    </el-card>
  </div>
</template>
<script>
  import api from 'common/src/api/graduationManagement/graduationManage';
  import {completeGetGraduateReviewBatch, completeQueryGraduateReviewAuthList, completeReviewAuthBatchClose, completeReviewAuthBatchOpen} from 'common/src/api/graduationManagement/graduationManage';
 
  import {
    forMateTimeToStr,
    changeTimeToYmdhm
  } from '../../components/gradMangeUtis';
  export default {
    name: 'graduationReviewRuth',
    data() {
      let currentYear = new Date().getFullYear();
      return {
        options: {
          auditBatch: []
        },
        condition: {
          graduationYear: '',
          auditBatch: ''
        },
        isOverseas: '1',
        tableData: [],
        PaginationParam: {
          total: 0,
          pageNum: 1,
          pageSize: 20
        },
        auditAuthStatusMap: {
          0: {
            label: this.$t('graduationRuth.unOpen'),
            type: 'danger'
          },
          1: {
            label: this.$t('graduationRuth.opened'),
            type: 'success'
          },
          2: {
            label: this.$t('graduationRuth.closed'),
            type: 'info'
          }
        },
        batchStatusMap: {
          0: {
            label: this.$t('graduationRuth.unSubmit'),
            type: 'danger'
          },
          1: {
            label: this.$t('graduationRuth.submited'),
            type: 'info'
          }
        }
      }
    },
    auditBatchCaches: {},
    mounted() {
      this.$options.auditBatchCaches = {};
      this.query();
    },
    methods: {
      indexMethod(index) {
        let t = (index + 1) + (this.PaginationParam.pageNum - 1) * this.PaginationParam.pageSize;
        if (t < 10) {
          return `0${t}`;
        }
        return t;
      },
      search() {
        this.PaginationParam.pageNum = 1;
        this.query();
      },
      query({
        pageSize,
        pageNum
      } = {}) {
        let params = {
          pageNum_: pageNum || this.PaginationParam.pageNum,
          pageSize_: pageSize || this.PaginationParam.pageSize,
          isOverseas: this.isOverseas
        }
        if (this.condition.graduationYear) {
          params.grade = this.condition.graduationYear;
        }
        if (this.condition.auditBatch) {
          params.reviewBatch = this.condition.auditBatch;
        }
        this.checkList = [];
        completeQueryGraduateReviewAuthList(params).then(res => {
          if (res && res.code === 200) {
            let data = res.data || {};
            let list = data.list || [];
            this.updateTableData(list);
            this.tableData = list;
            this.PaginationParam.total = data.total_;
            this.PaginationParam.pageNum = data.pageNum_;
            this.PaginationParam.pageSize = data.pageSize_;
          }
        }, () => {
          this.tableData = [];
          this.PaginationParam.total = 0;
          this.PaginationParam.pageNum = 1;
          this.PaginationParam.pageSize = params.pageSize_;
        });
      },
      reset() {
        this.$refs.conditionForm.resetFields();
        this.options.auditBatch = [];
        this.PaginationParam.pageNum = 1;
        this.query();
      },
      selectChange() {
        let graduationYear = this.condition.graduationYear;
        this.options.auditBatch = [];
        this.condition.auditBatch = '';
        if (!graduationYear) {
          return;
        }
        let param = {
          isOverseas: this.isOverseas,
          graduateData: graduationYear
        }
        if (this.$options.auditBatchCaches[graduationYear] && this.$options.auditBatchCaches[graduationYear].length) {
          this.setAuditBatchOption(graduationYear);
          return;
        }
        completeGetGraduateReviewBatch(param).then(res => {
          if (res && res.code === 200) {
            let data = res.data && res.data.list || [];
            this.$options.auditBatchCaches[graduationYear] = [];
            data.forEach((item) => {
              let existItem = _.find(this.$options.auditBatchCaches[graduationYear], specItem => {
                return specItem.key === item.reviewBatch;
              })
              if (!existItem) {
                this.$options.auditBatchCaches[graduationYear].push({
                  key: item.reviewBatch,
                  value: item.reviewBatch,
                  label: item.reviewBatchI18n
                });
              }
            })
            this.setAuditBatchOption(graduationYear);
          }
        })
      },
      setAuditBatchOption(key) {
        let values = this.$options.auditBatchCaches[key] || [];
        this.options.auditBatch = values;
      },
      singelOperate(data, flag) {
        let id = [data.id];
        if (!id) return;
        if (flag) {
          this.operateBatch('open', id);
        } else {
          this.operateBatch('', id);
        }
      },
      operateBatch(type, ids) {
        ids = ids || this.checkList;
        if (!ids || !ids.length) {
          this.$message({
            message: this.$t('graduationRuth.selectTip'),
            type: 'warning'
          });
          return;
        }
        let param = {
          isOverseas: this.isOverseas,
          ids
        };
        let promise, msg;
        if (type === 'open') {
          if (this.operateBeforeChecked(ids, '1')){
            this.$message({
              message: this.$t('graduationRuth.batchOpenValidErrorTip'),
              type: 'warning'
            });
            return;
          };
          msg = this.$t('graduationRuth.batchOpenSuccessTip');
          promise = completeReviewAuthBatchOpen(param);
        } else {
          if (this.operateBeforeChecked(ids, '2')){
            this.$message({
              message: this.$t('graduationRuth.batchCloseValidErrorTip'),
              type: 'warning'
            });
            return;
          };
          msg = this.$t('graduationRuth.batchCloseSuccessTip');
          promise = completeReviewAuthBatchClose(param);
        }
        promise.then(res => {
          if (res && res.code === 200){
            this.$message({
              message: msg,
              type: 'success'
            });
            this.query();
          }
        })
      },
      pageChange(page) {
        this.query(page);
      },
      checkBoxChange(checkArr) {
        let ids = [];
        checkArr.forEach(item => {
          ids.push(item.id);
        });
        this.checkList = ids;
      },
      operateBeforeChecked(ids, status) {
        let flag = false;
        ids.forEach(id => {
          let current = this.tableData.find(item =>{
            return item.id === id; 
          });
          let currentStatus = current && current.authorityStatus || '';
          if (currentStatus === status) {
            flag = true; 
          }
        })
        return flag;
      },
      updateTableData(data) {
        let currentTime = new Date().getTime();
        data.forEach(item => {
          let startTime = forMateTimeToStr(item.collegeReviewStartDate);
          let endTime = forMateTimeToStr(item.collegeReviewEndDate);
          if (currentTime < startTime || currentTime > endTime) {
            item.isOperateBtnDisabled = true;
          }else {
            item.isOperateBtnDisabled = false;
          }
          if (item.collegeReviewStartDate) {
            item.collegeReviewStartDate = changeTimeToYmdhm(item.collegeReviewStartDate);
          }
          if (item.collegeReviewEndDate) {
            item.collegeReviewEndDate = changeTimeToYmdhm(item.collegeReviewEndDate)
          }
        })
      },
      selectable(row, index) {
        if (row.isOperateBtnDisabled) {
          return false;
        }else {
          return true;
        }
      }
    }
  }

</script>
<style lang="scss" scoped>
  .graduationReview-title {
    line-height: 32px;
    margin-bottom: 0;
  }

</style>
